<?php


function options_for_select($valsTexts = array(), $selectedVal = null) {
	$opts = '';
	foreach ($valsTexts as $val => $text) {
		//make sure u dont put a number as the key name make it a str instead
		if (is_int($val)) { $val = $text; }
		if ($val[0] == '0') { $val = substr($val,1); }
		$opts .= '<option value="' . $val . '"';
		if ($val == $selectedVal) {	$opts .= ' selected="selected"'; }
		$opts .=  ' >' . $text . '</option>';
	}
	return $opts;

}

function select($obj_name, $param, $choices = array(), $options = array()) {
	$name = $obj_name . '[' . $param . ']';
	if (isset($options['include_blank'])) { $include_blank = true; unset($options['include_blank']); }
	global $this_copy;
	$sel_val = isset($this_copy->$obj_name->$param) ? $this_copy->$obj_name->$param : null;
	$opt_str = '';
	if (isset($include_blank)) { $opt_str .= '<option value="">Choose one</option>'; }
	$opt_str .= options_for_select($choices, $sel_val);
	select_tag($name, $opt_str, $options);
}

function select_tag($name, $preGeneratedOpts = '', $options = array()) {
	$select_str = '<select ';
	if (isset($options['muntiple'])) {	$name .= '[]'; $select_str .= 'multiple="multiple" '; unset($options['muntiple']); }
	if (isset($options['disabled'])) {	$select_str .= 'disabled="disabled" '; unset($options['disabled']); }
	$select_str .= "name='$name'".generate_html_options($options).'>'.$preGeneratedOpts.'</select>';
	echo $select_str;
}



function text_field($obj_name, $param, $options = array()) {
	global $this_copy;
	$id = $obj_name . '_' . $param;
	$val = isset($this_copy->$obj_name->$param) ? $this_copy->$obj_name->$param : "";
	echo "<input name='{$obj_name}[{$param}]' type='text' id='{$id}' value='{$val}' /><br /><br />";
}



function text_field_tag($name, $value = null, $options = array()) {
	echo "<input name='{$name}' type='text' id='{$name}' value='{$value}'",generate_html_options($options)," />";
}



function text_area_tag($name, $content = '', $options = array()) {
	$str = "<textarea name='{$name}'";
	if (isset($options['size'])) {
		$colsRows = explode('x', $options['size']);
		$colsRows = array_map('intval', $colsRows);
		$str .= " cols='{$colsRows[0]}' rows='{$colsRows[1]}'";
		unset($options['size']);
	}
	if (isset($options['disabled']) && ($options['disabled'] === true)) { $str .= ' disabled=disabled'; }
	echo $str, generate_html_options($options), " >{$content}</textarea>";
}


function submit_tag($value, $options = array())  {
	if (empty($value)) {$value = 'Save'; }
	$other_attrs = '';
	if (isset($options['disabled'])) {
		$other_attrs .= ($options['disabled'] === true) ? ' disabled=disabled' : " onclick='this.disabled=true;this.value=\"{$options['disabled']}\";this.form.submit();'";
		unset($options['disabled']);
	}
	$other_attrs .= generate_html_options($options);
	echo "<input {$other_attrs} name='commit' type='submit' value='{$value}' />";
}



function form_for() {
}


function start_form_tag($sendtourl, $options = array()) {
	if (is_array($sendtourl)) {
		$path_construct = '';
		if (array_key_exists('controller', $sendtourl)) {
			$path_construct .= $sendtourl['controller'] . '/';
			unset($sendtourl['controller']);
		}
		if (array_key_exists('action', $sendtourl)) {
			$path_construct .= $sendtourl['action'] . '/';
			unset($sendtourl['action']);
		}
		else {
			echo 'action for form must be specified';
			return false;
		}
		if (array_key_exists('id', $sendtourl)) {
			$path_construct .= $sendtourl['id'] . '/';
			unset($sendtourl['id']);
		}
		if (!empty($sendtourl)) {
			$path_construct .= '?';
			foreach($sendtourl as $key => $val) {
				$path_construct .= $key . '=' . $val;
			}
		}
		$sendtourl = $path_construct;
	}
	$form_construction = '<form action="' . PUBLIC_HTML . $sendtourl . '"';
	$form_construction .= (array_key_exists('multipart', $options) && $options['multipart'] == true) ? ' enctype="multipart/form-data"' : ' enctype="application/x-www-form-urlencoded"';
	$form_construction .= (array_key_exists('method', $options) && $options['method'] == 'get') ? ' method="get"' : ' method="post"';
	unset($options['multipart']);
	unset($options['method']);


	echo $form_construction . generate_html_options($options) . ' >';
}

function end_form_tag() { echo '</form>'; }

function label_tag($name, $text = null, $options = array()) {
	if ($text == null) { $text = ucfirst($name); }
	echo '<label for="',$name.'"',generate_html_options($options),'>',$text,'</label><br />';
}

function label($object_name, $method, $text = null, $options = array()) {
	$for = $object_name . '_' . $method;
	if ($text == null) { $text = ucfirst($method); }
	return label_tag($for, $text, $options = array());
}



function check_box_tag($name, $value = false, $checked = false, $options = array()) {
	if (!$value) { $value = $name; }
	$opts = generate_html_options($options);
	$chekd = ($checked) ? "checked='checked'" : "";
	echo "<input {$chekd} name='{$name}' type='checkbox' value='{$value}'{$opts} />";
}


function generate_html_options($html_opts = array()) {
	$html_options = '';
	foreach ($html_opts as $key => $value) { $html_options .= " $key='$value'"; }
	return $html_options;
}


function radio_button_tag($name, $value, $checked = false, $options = array()) {
	$opt_str = '';
	if (isset($options['disabled'])) { $opt_str .= ' disabled="disabled"'; unset($options['disabled']); }
	$opt_str .= generate_html_options($options);
	if ($checked) { $opt_str .= ' checked="checked"'; }
	echo '<input type="radio" name="',$name,'" value="',$value,'"',$opt_str,' />';
}



?>